package com.css.msgcenter.admin.msgapp.action;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.action.DirDict;
import com.css.core.model.IServiceResult;
import com.css.msgcenter.Results;
import com.css.msgcenter.admin.msgapp.service.MsgAppService;
import com.css.msgcenter.server.model.MsgApp;
import com.css.util.Ajax;
import com.css.util.DesUtil;
import com.css.util.Md5Util;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

public class AddMsgApp extends AdminAction implements ModelDriven{
	private static Log log = LogFactory.getLog(DirDict.class);
	private String uuid;
	private MsgApp msgApp;
	private String mailSmtpHost;
	private String mailUser;
	private String mailPassword;
	private String ips;
	
	public AddMsgApp() {
		msgApp = new MsgApp();
	}
	@Override
	public String adminGo() {
		
		try {
			if(StringHelper.isEmpty(ips))
				ips="";
			Timestamp createTime = new Timestamp(System.currentTimeMillis()); 
			msgApp.setCreatedTime(createTime);
			String memo = "{\"mail.smtp.host\":\"" + mailSmtpHost + "\",\"mail.user\":\"" + mailUser +
				"\",\"mail.password\":\"" + DesUtil.encrypt(mailPassword) + "\"}";
			msgApp.setMemo(memo);
			String MsgType = "";
			if(StringHelper.isNotEmpty(msgApp.getCheckMsgType())){
				List<String> MsgTypeList = StringHelper.strToList(msgApp.getCheckMsgType());
				for(int i = 0; i < MsgTypeList.size(); i++){
					MsgType += MsgTypeList.get(i).trim() + ",";
				}
				MsgType=MsgType.substring(0, MsgType.length()-1);
			}
			msgApp.setCheckMsgType(MsgType);
			msgApp.setUuid(Md5Util.getMD5(msgApp.getAppCode()));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result = Ajax.JSONResult(Results.ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
		IServiceResult<?> res = MsgAppService.addMsgApp(msgApp,ips);
		result = Ajax.JSONResult(res.getResultCode(), res.getResultDesc(),res.getResultObject());
		return res.toActionResult();
	}
	@Override
	public Object getModel() {
		return msgApp;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public MsgApp getMsgApp() {
		return msgApp;
	}
	public void setMsgApp(MsgApp msgApp) {
		this.msgApp = msgApp;
	}
	public String getMailSmtpHost() {
		return mailSmtpHost;
	}
	public void setMailSmtpHost(String mailSmtpHost) {
		this.mailSmtpHost = mailSmtpHost;
	}
	public String getMailUser() {
		return mailUser;
	}
	public void setMailUser(String mailUser) {
		this.mailUser = mailUser;
	}
	public String getMailPassword() {
		return mailPassword;
	}
	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}
	public String getIps() {
		return ips;
	}
	public void setIps(String ips) {
		this.ips = ips;
	}

}